﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;

namespace yao.Mvc
{
    public class PageHelper
    {
        public PageHelper(HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routes, long totalCount, int pageIndex,int pageSize )
        {
            NubersDic = new Dictionary<int, string>();

            HtmlHelper = htmlHelper;
            ActionName = actionName;
            Routes = routes;
            ControllerName = controllerName;
            TotalCount = totalCount;
            PageCount = Convert.ToInt32(totalCount/pageSize);
            if (totalCount % pageSize != 0) {
                PageCount++;
            }

            PageSize = pageSize;
            PageIndex = pageIndex;
        }

        public HtmlHelper HtmlHelper { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }

        public RouteValueDictionary Routes { get; set; }

        public long TotalCount { get; set; }

        /// <summary>
        /// 页面总算
        /// </summary>
        public int PageCount { get; set; }
        /// <summary>
        /// 页容量
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get; set; }

        private int _PageNumber = 10;


        private Dictionary<int, string> NubersDic { get; set; }
        private string GenerateUrl()
        {
            return UrlHelper.GenerateUrl(null, ActionName, ControllerName, null, null, null, Routes, HtmlHelper.RouteCollection, HtmlHelper.ViewContext.RequestContext, true);
        }
        /// <summary>
        /// 生成数字
        /// </summary>
        private void GenerateNuber()
        {
            int start = PageIndex - _PageNumber / 2;
            int end = PageIndex + _PageNumber / 2;
            if (start < 0)
            {
                end -= start;
                start = 0;
            }
            if (end > PageCount - 1)
            {
                start -= end - (PageCount - 1);
                end = PageCount - 1;
            }
            start = Math.Max(1, start + 1);
            end = Math.Min(end + 1, PageCount);

            for (int i = start; i < end; i++)
            {
                Routes["p"] = i;
                NubersDic.Add(i, GenerateUrl());
            }
        }
        
        public string GeneratePager() {
            GenerateNuber();
            if (NubersDic.Count <= 0) {
                return string.Empty;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("共 <em class=\"c7\">{0}</em>条&nbsp;&nbsp;每页",TotalCount);
            sb.AppendFormat("<select>");
            sb.AppendFormat("<option value =\"20\" >20</ option> ");
            sb.AppendFormat("<option value=\"30\">30</option>");
            sb.AppendFormat("<option value = \"50\">50</option>");
            sb.AppendFormat("</select> 条");
            foreach (KeyValuePair<int,string> key in NubersDic)
            {
                if (key.Key == PageIndex)
                {
                    sb.AppendFormat("<span>{0}</span>", key.Key);
                }
                else
                {
                    sb.AppendFormat("<a href=\"{0}\">{1}</a>", key.Value, key.Key);
                }
            }
            return sb.ToString();
        }

    }
}
